package robot.capture;

import com.sun.awt.AWTUtilities;

import javax.swing.*;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

public class ShowCapture {

    JFrame frame = null;
    int WIDTH = 500;
    int HEIGHT = 500;
    JLabel label = null;

    public ShowCapture(final String url) {
        frame = new JFrame();
        label = new JLabel();
        final java.util.Timer timer = new Timer();
        TimerTask task = new TimerTask() {

            File file = new File(url);
            File[] files = file.listFiles();
            final int size = files.length;
            int index = 0;

            @Override
            public void run() {
                if (index < size - 1) {
                    label.setText("");
                    Image image = Toolkit.getDefaultToolkit().getImage(files[index].getPath());
                    ImageIcon icon = new ImageIcon();
                    image = image.getScaledInstance((int) FrameUtils.getScreenWidth(), (int) FrameUtils.getScreenHeight(), Image.SCALE_FAST);
                    icon.setImage(image);
                    label.setIcon(icon);
                    index++;
                } else {
                    timer.cancel();
                    frame.getContentPane().setBackground(Color.BLACK);
                    AWTUtilities.setWindowOpacity(frame, 0.5f);
                    frame.remove(label);
                    JOptionPane.showMessageDialog(frame, " 播放完毕！");
                    frame.dispose();
                }
            }
        };
        timer.schedule(task, 1000, 200);

        frame.setUndecorated(true);
        frame.add(label);
        frame.setBounds(0, 0, (int) FrameUtils.getScreenWidth(), (int) FrameUtils.getScreenHeight());
        frame.setVisible(true);
        frame.setDefaultCloseOperation(3);
        frame.setResizable(false);

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        toolkit.addAWTEventListener(new AWTEventListener() {

            public void eventDispatched(AWTEvent event) {
                if (event.getClass() == KeyEvent.class) {
                    KeyEvent e = (KeyEvent) event;
                    int key = e.getKeyCode();
                    if (key == 27) {
                        timer.cancel();
                        frame.dispose();
                    }
                }
            }

        }, 1000);
    }


    public static void main(String[] args) {
        new ShowCapture("E:\\data\\capture\\1555735899999");
    }
}
